import 'reflect-metadata';
import { Response, Request, NextFunction } from 'express';
import { get, post, controller, use } from '../decorator';
import { getResponseData } from '../utils/util';
import Crowller from '../utils/crowller';
import Anaylzer, { DetailAnaylzer } from '../utils/analyzer';
import { BodyRequest } from './LoginController';
import { checkLogin } from './LoginController';

const BASE_URL = 'http://www.biquge.info/72_72318/';

@controller('/api')
export class CrowllerController {
    @get('/getArticleList')
    @use(checkLogin)
    async getArticleList(req: BodyRequest, resp: Response) {
        try {
            const anaylzer = Anaylzer.getInstance();
            const crowller = new Crowller(BASE_URL, anaylzer);
            const resultJson = await crowller.initSpliderProcess();
            resp.json(getResponseData(JSON.parse(resultJson)));
        } catch (e) {
            resp.json(getResponseData(null, e));
        }
    }

    @get('/getArtileDetail')
    @use(checkLogin)
    async getArtileDetail(req: Request, resp: Response) {
        console.log('1232132',req.query)
        const { link } = req.query;
        if (!link) return resp.json(getResponseData(null, '链接不能为空'))
        try {
            const anaylzer = DetailAnaylzer.getInstance();
            const crowller = new Crowller(BASE_URL + link, anaylzer);
            const resultJson = await crowller.initSpliderProcess();
            resp.json(getResponseData(JSON.parse(resultJson)));
        } catch (e) {
            resp.json(getResponseData(null, e));
        }
    }

}